﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using WX.WMS.DataModel;
using WX.WMS.Common;
using WX.WMS.BLL;

public partial class CK_Products_NQ_Select_Products_Window :  PageBase
{
    private string _key
    {
        get
        {
            return Convert.ToString(ViewState["_key"]);
        }
        set
        {
            ViewState["_key"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBandStockList();
            DataBandAreaList(this.ddlStock.SelectedValue);
            DataBandFloorList(this.ddlArea.SelectedValue);
            searchDand();
        }

    }
    protected void searchButton_Click(object sender, ImageClickEventArgs e)
    {
        //string strKey = "";
        //string strBarCode = "";
        //strKey = Key.Text.ToString();
        //strBarCode = barcode.Text.Trim();
        ////searchDand(strType, strBrand, strKey);
        //Response.Redirect(string.Format("CK_NQ_Select_Products_Window.aspx?action=search&Key={0}&BarCode={1}", strKey, strBarCode));

        searchDand();
    }

    protected void searchDand()
    {
        StoreBLL BllStore = new StoreBLL();
      // object key= Request.QueryString["Key"];
        object key = this.Key.Text.Trim();
       if (key != null && key != "")
       {
           BllStore.AddLikeCondition("T_PRODUCT.Name",key.ToString());
       }

       if (!string.IsNullOrEmpty(standard.Text))
       {
           BllStore.AddLikeCondition("T_PRODUCT_DETAIL.STANDARD", standard.Text);
       }

       if (!string.IsNullOrEmpty(keystandard.Text))
       {
           BllStore.AddLikeCondition("T_PRODUCT_DETAIL.KeyStandard", keystandard.Text);
       }

       if (!string.IsNullOrEmpty(lot.Text))
       {
           BllStore.AddLikeCondition("T_PRODUCT_DETAIL.LOT_NUMBER", lot.Text);
       }

       if (!string.IsNullOrEmpty(supplylot.Text))
       {
           BllStore.AddLikeCondition("T_PRODUCT_DETAIL.SupplyLotNumber", supplylot.Text);
       }



       //object BarCode = Request.QueryString["BarCode"];
       object BarCode = this.barcode.Text.Trim();
       if (BarCode != null && BarCode != "")
       {
           BllStore.AddLikeCondition("T_PRODUCT_DETAIL.BAR_CODE", BarCode.ToString());
       }
       var search = BllStore.GetStoreList();
       if (this.ddlStock.SelectedValue != "-1")
       {
           search = search.Where(t => t.T_FLOOR.T_STOCK_AREA.STOCK_ID.ToString() == this.ddlStock.SelectedValue);
       }
       if (this.ddlArea.SelectedValue != "-1")
       {
           search = search.Where(t => t.T_FLOOR.AREA_ID.ToString() == this.ddlArea.SelectedValue);
       }
       if (this.ddlFloor.SelectedValue != "-1")
       {
           search = search.Where(t => t.T_FLOOR.FloorID.ToString() == this.ddlFloor.SelectedValue);
       }

       SearchResult.DataSource = search.ToList();
       SearchResult.DataBind();
    }


    protected void SearchResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string BarCode = SearchResult.DataKeys[e.RowIndex]["ID"].ToString();

        Jscript.CloseWindowReturnValues(BarCode);
        return;

    }

    protected void DataBandStockList()
    {
        this.ddlStock.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        this.ddlStock.DataSource = storeHouseBLL.GetStockList();
        this.ddlStock.DataBind();
        this.ddlStock.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandAreaList(string StockID)
    {
        this.ddlArea.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        if (StockID != "-1")
        {
            this.ddlArea.DataSource = storeHouseBLL.GetStockAreaList(StockID);
        }
        this.ddlArea.DataBind();
        this.ddlArea.Items.Insert(0, new ListItem("请选择", "-1"));
    }

    protected void DataBandFloorList(string AreaID)
    {
        this.ddlFloor.Items.Clear();
        StoreHouseBLL storeHouseBLL = new StoreHouseBLL();
        if (AreaID != "-1")
        {
            this.ddlFloor.DataSource = storeHouseBLL.GetFloorList(AreaID);
        }
        this.ddlFloor.DataBind();
        this.ddlFloor.Items.Insert(0, new ListItem("请选择", "-1"));
    }
    protected void ddlStock_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandAreaList(this.ddlStock.SelectedValue);
        DataBandFloorList(this.ddlArea.SelectedValue);
    }
    protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataBandFloorList(this.ddlArea.SelectedValue);
    }
    protected void SearchResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.SearchResult.PageIndex = e.NewPageIndex;
        searchDand();
    }
}
